package com.sandmens.droidvdr;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.URLUtil;
import android.widget.Toast;
import android.widget.VideoView;



public class player extends Activity
{
	private VideoView mVideoView = null;
	private String URL_stream = "";
	private File File_Handler;
    private ProgressDialog myProgressDialog = null;
    private String Error_Description ="";
    private String URL_mvdr = "";
  	private String myLine = ""; 
  	private Boolean VLC_is_Stoped = false;
	
    @Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
        getWindow().setFormat(PixelFormat.TRANSLUCENT);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.viewplayer);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        mVideoView = (VideoView) findViewById(R.id.VideoView);
        URL_stream = getIntent().getExtras().getString("STREAM_URL");
        URL_mvdr = getIntent().getExtras().getString("URL");
        
	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if ( keyCode == KeyEvent.KEYCODE_BACK)
		{
			try 
			{
				if ( mVideoView.isPlaying() )
				{
					mVideoView.stopPlayback();
				}
			} 
			catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			this.setResult(Activity.RESULT_OK);
		}			
		return super.onKeyDown(keyCode, event);
	}

	@Override
	protected void onRestart() {
		// TODO Auto-generated method stub
		super.onRestart();
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}

	@Override
	protected void onStart() {
		super.onStart();
		if (URL_stream.contains("://") )
		{
			if ( !mVideoView.isPlaying())
			{
				mVideoView.setVideoURI(Uri.parse(URL_stream));
				mVideoView.start();
				mVideoView.requestFocus();
			}
		}
		else
		{
			if ( !mVideoView.isPlaying())
			{
				mVideoView.setVideoPath(URL_stream);
				mVideoView.start();
				mVideoView.requestFocus();
			}
		}
	}

	@Override
	protected void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		// TODO Auto-generated method stub
		if ( mVideoView.isPlaying() )
		{
			mVideoView.stopPlayback();
		}
		super.onPause();
	}
    
 	private void read_stop_vlc()
  	{
  		VLC_is_Stoped = false;
        File_Handler = new File
  		(Environment.getExternalStorageDirectory().getPath()+"/mvdr/stop.htm" );
        try 
        {
                if (File_Handler.exists() && File_Handler.canRead() && File_Handler.length() > 0) 
                {
                    try {
	                    BufferedReader r = new BufferedReader( new InputStreamReader( new FileInputStream(File_Handler), "UTF8" ) );
	                    while ((myLine =r.readLine()) != null)
						{
						        if (myLine.contains("done stop :-)") )
						        {
						        	VLC_is_Stoped = true;
						        }
						}
					} 
                    catch (IOException ex) 
					{
                    	System.out.println(ex.toString());
					}
                }
        }
        finally 
        {
        }
  	}

  	private Boolean downloadFile(Integer download_mode, String URL_for_VDR)
  	{
  		Error_Description = "";
    	switch (download_mode)
    	{
    		// VLC is Stoping
    		case 3:
    	  		try
    	  		{
    	  			URL url_mvdr = new URL(URL_mvdr + "?stop");
    	  	        HttpURLConnection connection_mvdr = (HttpURLConnection) url_mvdr.openConnection();
    	  	        connection_mvdr.setConnectTimeout(10000);
    	  	        connection_mvdr.setRequestMethod("GET");
    	  	        connection_mvdr.setDoOutput(true);
    	  	        connection_mvdr.connect();

    	  	        File File_Bouquet = new File
    	  	  		(Environment.getExternalStorageDirectory().getPath()+"/mvdr/stop.htm" );

    	  	  		if (!File_Bouquet.exists())
    	  	  			File_Bouquet.createNewFile();
    	  	  		
    	  	        FileOutputStream fileOutput = new FileOutputStream(File_Bouquet);
    	  	        InputStream input_mvdr = connection_mvdr.getInputStream();
	    	  	        
    	  	        byte[] buffer = new byte[1024];
    	  	        int len1 = 0;
    	  	        while ( (len1 = input_mvdr.read(buffer)) != -1 ) {
    	  	        	fileOutput.write(buffer,0, len1);
    	  	        }
    	  	        
    	  	        fileOutput.close();
    	  	        input_mvdr.close();
    	  	        connection_mvdr.disconnect();

    	  	        
    	  		}
    	  		catch (IOException ex) 
    	  		{
                	System.out.println(ex.toString());
                	Error_Description = ex.getMessage().toString();
    	            return false;
    	  		}
    			return true;
    			
    	}
		return true;
  	}

	

}
